package com.shujichen.enums;

import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 任务状态枚举类型
 */
public enum TaskStatusEnum {

    WAITING("0", "等待中"),
    RUNNING("1", "执行中"),
    SUCCESS("2", "执行成功"),
    FAILED("3", "执行失败"),
    TIMEOUT("4", "执行超时");

    private final String code;

    private final String desc;

    TaskStatusEnum(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    public static TaskStatusEnum getEnumByCode(String code) {
        if (StringUtils.hasText(code)) {
            TaskStatusEnum[] values = TaskStatusEnum.values();
            for (TaskStatusEnum typeEnum : values) {
                if (typeEnum.getCode().equalsIgnoreCase(code)) {
                    return typeEnum;
                }
            }
        }
        return null;
    }

    public static TaskStatusEnum getEnumByDesc(String desc) {
        if (StringUtils.hasText(desc)) {
            TaskStatusEnum[] values = TaskStatusEnum.values();
            for (TaskStatusEnum typeEnum : values) {
                if (typeEnum.getDesc().equalsIgnoreCase(desc)) {
                    return typeEnum;
                }
            }
        }
        return null;
    }

    public static Map<String, String> getEnumMap() {
        return Arrays.stream(TaskStatusEnum.values()).collect(Collectors.toMap(TaskStatusEnum::getCode, TaskStatusEnum::getDesc));
    }

}
